//
//  AEPointsAcquireMainView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/17.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEPointsAcquireMainView.h"
#import "AE_Solar-Swift.h"
#import "AEPointsAcquireViewModel.h"

@interface AEPointsAcquireMainView ()

@property (nonatomic, strong) AEPointsAcquireViewModel *viewModel;

@property (nonatomic, strong) UIView *topView;

@property (nonatomic, strong) UIImageView *tagImgView;

@property (nonatomic, strong) UILabel *leftTitleLabel;
@property (nonatomic, strong) UILabel *rightTitleLabel;
@end

@implementation AEPointsAcquireMainView

- (void)initWithSubView {
    [super initWithSubView];
    
    self.backgroundColor = kWhiteColor;
    self.layer.cornerRadius = 18;
    self.layer.masksToBounds = YES;
    
    [self initWithtopView];
}

- (void)initWithtopView {
    if (!_topView) {
        CGRect frame = CGRectMake(12, 14, self.width - 12 * 2, 30);
        _topView = [[UIView alloc] initWithFrame:frame];
        _topView.backgroundColor = [UIColor colorWithHexString:@"#FFF4CA"];
        _topView.layer.cornerRadius = 7;
        _topView.layer.masksToBounds = YES;
        [self addSubview:_topView];
    }
}

- (void)initWithTagImgView {
    if (!_tagImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_points_task_tag"];
        CGRect frame = CGRectMake(6, (self.topView.height - 18) * 0.5, 18, 18);
        _tagImgView = [UIImageView createWithImageView:self.topView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.backgroundColor = [UIColor orangeColor];
        }];
    }
}

- (void)initWithLeftTitleLabel {
    if (!_leftTitleLabel) {
        NSString *title = [AEStringExten localizedValueWithKey:@"完成任务领积分"];
        UIFont *font = [UIFont systemFontOfSize:10.5 weight:UIFontWeightBold];
        float width = [title widthForFont:font] + 2;
        CGRect frame = CGRectMake(self.tagImgView.right + 3.5, (self.topView.height - 20) * 0.5, width, 20);
        _leftTitleLabel = [UILabel createWithLabel:self.topView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#8A620B"];
            label.text = title;
        }];
    }
}

- (void)initWithRightTitleLabel {
    UIFont *font = [UIFont systemFontOfSize:10.5 weight:UIFontWeightBold];
    if (!_rightTitleLabel) {
        CGRect frame = self.leftTitleLabel.frame;
        _rightTitleLabel = [UILabel createWithLabel:self.topView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#8A620B"];
//            label.text = title;
        }];
    }
    
    NSString *titleLeft = [AEStringExten localizedValueWithKey:@"今天还可以领取"];
    NSString *titleRight = [AEStringExten localizedValueWithKey:@"个任务"];
    NSMutableAttributedString *mut = [[NSMutableAttributedString alloc] init];
    NSMutableAttributedString *leftMut = [EDSUtility attributeTitle:titleLeft titleColor:[UIColor colorWithHexString:@"#8A620B"] titleFont:font value:@"5" valueColor:kThemeColor valueFont:font];
    NSMutableAttributedString *rightMut = [EDSUtility attributeTitle:titleRight titleColor:[UIColor colorWithHexString:@"#8A620B"] titleFont:font value:@"" valueColor:[UIColor colorWithHexString:@"#8A620B"] valueFont:font];
    [mut appendAttributedString:leftMut];
    [mut appendAttributedString:rightMut];
    
    float width = [[mut string] widthForFont:font] + 2;
    self.rightTitleLabel.width = width;
    self.rightTitleLabel.left = self.topView.width - 10 - width;
    self.rightTitleLabel.attributedText = mut;
}

- (void)updateWithViewData:(id)data {
    
    self.viewModel = data;
    
    [self initWithTagImgView];
    
    [self initWithLeftTitleLabel];
    
    [self initWithRightTitleLabel];
    
    
    CGRect frame = CGRectMake(12, self.topView.bottom + 6, self.width - 12 * 2, kAEPointsAcquireMainSingleViewHeight);
    AEPointsAcquireMainSingleView *preView = nil;
    for (int i=0; i<self.viewModel.dataList.count; i++) {
        TYZCommonCellModel *model = self.viewModel.dataList[i];
        if (preView) {
            frame.origin.y = preView.bottom;
        }
        AEPointsAcquireMainSingleView *view = [[AEPointsAcquireMainSingleView alloc] initWithFrame:frame];
        [self addSubview:view];
        [self.viewList addObject:view];
        preView = view;
        [view updateWithViewData:model];
        @weakify(self);
        view.touchGoFinishBlock = ^{
            @strongify(self);
            if (self.touchGoFinishBlock) {
                self.touchGoFinishBlock(model);
            }
        };
    }
    
    AEPointsAcquireMainSingleView *lastView = self.viewList.lastObject;
    lastView.bottomLineHidden = YES;
}

@end
